package longrise.phone.com.bjjt_henan.compensate;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.main.NewMainView;
import longrise.phone.com.bjjt_henan.myaccident.AccidentMainView;

/**
 * 保险报案，选择是否向保险公司报案，Created by Administrator on 2016/5/12.
 */

public class InsuranceReportChoiceView extends LFView implements View.OnClickListener, ILFMsgListener {
    private Context context = null;
    private View view = null;
    private Button bt_toinsurance;
    private Button bt_toinsurance1;
    private EntityBean[] CreateAgreementbean;
    private String appCaseno;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private String ifSingle = "-1";

    public InsuranceReportChoiceView(Context c, EntityBean[] CreateAgreementbean, String appCaseno) {
        super(c);
        this.context = c;
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
    }

    public void setIfSingle(String ifSingle) {
        this.ifSingle = ifSingle;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        this.addILFMsgListener(this);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_insurancereportchoiceview, null);
        bt_toinsurance = (Button) view.findViewById(R.id.bt_toinsurance);
        bt_toinsurance1 = (Button) view.findViewById(R.id.bt_toinsurance1);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("保险报案");
    }

    private void regEvent(boolean b) {
        if (null != bt_toinsurance) {
            bt_toinsurance.setOnClickListener(b ? this : null);
        }
        if (null != bt_toinsurance1) {
            bt_toinsurance1.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_toinsurance:
                if (checkDuty()) {
                    return;
                }
                //跳转温馨提示，将信息携带过去
                if (null != ifSingle && ifSingle.equals("0")) {//单车事故
                    PromptAfterInsuranceReportView promptafterinsurancereportview = new PromptAfterInsuranceReportView(context, CreateAgreementbean, appCaseno, "0");
                    FrameworkManager.getInstance().showNewForm(context, promptafterinsurancereportview);
                } else {
                    PromptAfterInsuranceReportView promptafterinsurancereportview = new PromptAfterInsuranceReportView(context, CreateAgreementbean, appCaseno);
                    FrameworkManager.getInstance().showNewForm(context, promptafterinsurancereportview);
                }
                break;
            case R.id.bt_toinsurance1:
                //不报案
                giveupCps();
                break;
            case R.id.hn_btn_back:
                closeForm();
                OnDestroy();
                break;

        }
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            if (!TextUtils.equals(ifSingle, "0")) {
                //只能生成一次责任认定书
                backHomeView();
            }
        }
        return null;
    }

    //检查责任情况，如果是同责则不允许保险报案
    private boolean checkDuty() {
        if (CreateAgreementbean != null && CreateAgreementbean.length > 0) {
            String duty = CreateAgreementbean[0].getString("dutytype");
            if (TextUtils.equals(duty, "2") || TextUtils.equals(duty, "3") || TextUtils.equals(duty, "4")) {
                UiUtil.showToast(context, "同责、主责或次责案件无法保险报案");
                return true;
            }
        }
        return false;
    }

    //放弃保险报案
    private void giveupCps() {
        DialogUtils.showDialog(context);
        String userflag = ((BaseApplication) context.getApplicationContext()).getKckpName();
        String token = ((BaseApplication) context.getApplicationContext()).getToken();
        ApiImpI.getInstance(context).giveupCps(appCaseno, userflag, token, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    //放弃成功则返回主页
                    backHomeView();
                    return;
                }
                String redes = bean.getString("redes");
                UiUtil.showToast(context, redes);
            }
        });
    }

    /**
     * 案件处理结束，回到主界面
     */
    private void backHomeView() {
        LFView view = null;
        boolean isHistroy = PrefUtils.getBoolean(context, MsgConstant.HISTORY_CASE, false);
        if (isHistroy) {//回到历史案件列表
            OnDestroy();
            closeForm();
            FrameworkManager.getInstance().closePopForm();
            view = new AccidentMainView(context);
        } else {
            FrameworkManager.getInstance().destroyAllForm();
            view = new NewMainView(context);
        }
        FrameworkManager.getInstance().showNewForm(context, view);
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        regEvent(false);
        removeILFMsgListener(this);
        CreateAgreementbean = null;
        view = null;
    }
}
